<?php
class My_View_Helper_MenuDepartmentHelper extends Zend_View_Helper_Abstract
{
    protected $_categories;
    protected $_rootHref = '';
    protected $_categoryId;

    public function __construct() {                
        $this->_categories = new Application_Model_Categories();
    }    

    public function menuDepartmentHelper($categoryId) {                
        echo $categoryId;
        $this->_buildMenu();
    }

    protected function _routeRules($type, $alias) {        
        $rules = array(
            'title'      => '',
            'static'     => '/static/' . $alias,
            'department' => $this->_rootHref.'/department/' . $alias,
            'feed'       => '/feed/'.$alias,            
            'custom'     => '/custom/'.$alias
        );
        return $rules[$type];
    }

    protected function _buildMenu() {
        $allParents = $this->_categories->getCategoriesByParentId($this->_categoryId);
        $menu = '';
        foreach ($allParents as $parent) {            
            $menu .= $this->_buildNode($parent);
        }        
        echo $menu;
    }

    protected function _getChildNodes($parentId, $rootHref) {        
        $children = $this->_categories->getCategoriesByParentId($parentId);                
        $childNode = '';
        if ($children) {                    
            $childNode .= '<ul>';
            foreach ($children as $child) {
                $childNode .= $this->_buildNode($child, $rootHref);                
            }
            $childNode .= '</ul>';
        }
        return $childNode;
    }

    protected function _buildNode(array $parent, $rootHref='') {
        $href  = $rootHref . $this->_routeRules($parent['type'], $parent['alias']);        
        $node  = '<li>';
        $node .= '<a href="'.$href.'"';
        $node .= '<a title="'.$parent['category_name'].'">';
        $node .= $parent['category_name'];
        $node .= '</a>';
        $href  = $rootHref?$rootHref:$href;
        $node .= $this->_getChildNodes($parent['id'], $href);
        $node .= '</li>';            
        return $node;
    }    
}